package com.aabte.hystrixlearn.controller;

import com.aabte.hystrixlearn.model.ProductInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Daniel
 */
@Slf4j
@RestController
@RequestMapping("/product")
public class ProductController {

    private Map<Long, ProductInfo> map;

    @PostConstruct
    public void setup() {
        map = new HashMap<>(4);

        map.put(1L, ProductInfo.builder().id(1L).name("product_1").build());
        map.put(2L, ProductInfo.builder().id(2L).name("product_2").build());
        map.put(3L, ProductInfo.builder().id(3L).name("product_3").build());
        map.put(4L, ProductInfo.builder().id(4L).name("product_4").build());
    }

    @GetMapping("/{id:\\d+}")
    public ProductInfo one(@PathVariable Long id) {
        log.info("Start one. id: {}", id);
        ProductInfo productInfo = map.get(id);
        log.info("End one. productInfo: {}", productInfo);
        return productInfo;
    }

}
